package com.yand.frame.utils;


import com.yand.frame.common.TreeBean;
import org.springframework.stereotype.Component;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

/**
 *  树的工具类，将list数据，解析成树的结构
 */
@Component
public class TreeUtil {


    /**
     * 将菜单解析成树形结构
     * @param list
     * @return
     */
    public List<? extends TreeBean> getAllTreeJavaBean(List<? extends TreeBean> list){
        List<TreeBean> treeBeansAllList =new  ArrayList<>();
        List<TreeBean> rootTreeBeanList = getRootTreeBeanList(list);

        //通过根节点获取子节点信息
        for(TreeBean treeBean : rootTreeBeanList){
            treeBean= getTreeChildList(treeBean,list);
            treeBeansAllList.add(treeBean);
        }

        return treeBeansAllList;
    }

    /**
     * 获取递归集合 类似 oracle中的
     * start..with..函数所查询出的集合
     * @param
     * @return
     */
    public List<? extends TreeBean> getRecursionList(List<? extends TreeBean> allList){
        List<TreeBean> recursionList =new  ArrayList();
        for(TreeBean treeBean : allList){
            if (treeBean.getParentId()==0) {//父级菜单开始添加
                recursionList.add(treeBean);
                if (ifChilds(allList, treeBean.getId())){
                    List<TreeBean> childs = new ArrayList<>();
                    childs = getRecursionChildList(allList,treeBean.getId(),childs);
                    recursionList.addAll(childs);

                }


            }
        }
        return recursionList;
    }


    //获取所有根节点信息
    private List<TreeBean> getRootTreeBeanList(List<? extends TreeBean> list){

        List<TreeBean> rootTreeBean = list.stream().filter((e)->((TreeBean) e).getParentId()==0).collect(Collectors.toList());
        return rootTreeBean;
    }

    //获取所有的子节点
    private  TreeBean getTreeChildList(TreeBean treeBean, List<? extends TreeBean> list){
        List<TreeBean> chilList =new  ArrayList<>();
        for(TreeBean treeBeanChild : list) {
            if(treeBeanChild.getParentId().intValue() == treeBean.getId().intValue()) {
                chilList.add(getTreeChildList(treeBeanChild, list));
            }
        }



        treeBean.setTreeBeanList(chilList);
        return treeBean;
    }


    private List<TreeBean> getRecursionChildList(List<? extends TreeBean> allList,int treeBeanID, List<TreeBean> childs){

        for (TreeBean testEntity : allList) {
            if (testEntity.getParentId()==treeBeanID) {//查询下级菜单
                childs.add(testEntity);
                if (ifChilds(allList, testEntity.getId())){
                    getRecursionChildList(allList, testEntity.getId(), childs);
                }
            }
        }
        return childs;

    }



    //判断是否存在子集
    private static boolean ifChilds(List<? extends TreeBean> list,int pId) {
        boolean flag = false;
        for (TreeBean testEntity : list) {
            if (testEntity.getParentId()==pId) {
                flag=true;
                break;
            }
        }
        return flag;
    }

}
